import matplotlib.pyplot as plt
import numpy as np

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False

# 创建图形和子图
fig, ax = plt.subplots(1, 1, figsize=(16, 10))
ax.set_xlim(0, 16)
ax.set_ylim(0, 10)
ax.axis('off')

# 绘制流程图节点
nodes = {
    'A': (2, 9, '异常发生'),
    'B': (2, 7.5, '数据采集'),
    'C': (2, 6, '特征提取'),
    'D': (2, 4.5, '评分引擎'),
    'E': (6, 4.5, '维度评估'),
    'F': (10, 4.5, '加权计算'),
    'G': (14, 4.5, '综合评分'),
    'H': (14, 7.5, '判定等级'),
    'I': (14, 9, '处理决策'),
    'J': (14, 3, '记录日志'),
    'K': (14, 1.5, '通知运维'),
    'L': (14, 0, '紧急处理'),
    'End': (10, 0, '结束')
}

# 绘制节点
for key, (x, y, label) in nodes.items():
    ax.add_patch(plt.Rectangle((x-1, y-0.3), 2, 0.6, facecolor='lightblue', edgecolor='black'))
    ax.text(x, y, label, ha='center', va='center', fontsize=10, weight='bold')

# 绘制评分维度子节点
dimensions = {
    'D1': (4, 6, '初始严重程度'),
    'D2': (4, 5, '持续时间'),
    'D3': (4, 4, '发生频率'),
    'D4': (4, 3, '业务影响'),
    'D5': (4, 2, '影响范围'),
    'D6': (4, 1, '趋势分析')
}

for key, (x, y, label) in dimensions.items():
    ax.add_patch(plt.Rectangle((x-1, y-0.3), 2, 0.6, facecolor='lightgreen', edgecolor='black'))
    ax.text(x, y, label, ha='center', va='center', fontsize=9)

# 绘制权重计算子节点
weights = {
    'F1': (8, 6, '初始严重程度×20%'),
    'F2': (8, 5, '持续时间×20%'),
    'F3': (8, 4, '发生频率×15%'),
    'F4': (8, 3, '业务影响×25%'),
    'F5': (8, 2, '影响范围×10%'),
    'F6': (8, 1, '趋势分析×10%')
}

for key, (x, y, label) in weights.items():
    ax.add_patch(plt.Rectangle((x-1.5, y-0.3), 3, 0.6, facecolor='lightyellow', edgecolor='black'))
    ax.text(x, y, label, ha='center', va='center', fontsize=8)

# 绘制连接线
# 主流程
ax.annotate('', xy=(2, 7.2), xytext=(2, 8.7), arrowprops=dict(arrowstyle='->', lw=1.5))
ax.annotate('', xy=(2, 5.7), xytext=(2, 7.2), arrowprops=dict(arrowstyle='->', lw=1.5))
ax.annotate('', xy=(2, 4.2), xytext=(2, 5.7), arrowprops=dict(arrowstyle='->', lw=1.5))

# 连接到维度评估
ax.annotate('', xy=(6, 4.5), xytext=(3, 4.5), arrowprops=dict(arrowstyle='->', lw=1.5))

# 维度评估到各维度
for i in range(1, 7):
    ax.annotate('', xy=(5, 7-i), xytext=(6, 4.5), arrowprops=dict(arrowstyle='->', lw=1))

# 各维度到权重计算
ax.annotate('', xy=(8, 4.5), xytext=(7, 4.5), arrowprops=dict(arrowstyle='->', lw=1.5))

# 权重计算到各权重项
for i in range(1, 7):
    ax.annotate('', xy=(6.5, 7-i), xytext=(8, 4.5), arrowprops=dict(arrowstyle='->', lw=1))

# 权重计算到综合评分
ax.annotate('', xy=(14, 4.5), xytext=(9.5, 4.5), arrowprops=dict(arrowstyle='->', lw=1.5))

# 综合评分到判定等级
ax.annotate('', xy=(14, 7.2), xytext=(14, 4.8), arrowprops=dict(arrowstyle='->', lw=1.5))

# 判定等级到处理决策
ax.annotate('', xy=(14, 8.7), xytext=(14, 7.8), arrowprops=dict(arrowstyle='->', lw=1.5))

# 处理决策到各处理方式
ax.annotate('', xy=(14, 2.7), xytext=(14, 8.7), arrowprops=dict(arrowstyle='->', lw=1))
ax.annotate('', xy=(14, 1.2), xytext=(14, 8.7), arrowprops=dict(arrowstyle='->', lw=1))
ax.annotate('', xy=(14, -0.3), xytext=(14, 8.7), arrowprops=dict(arrowstyle='->', lw=1))

# 各处理方式到结束
ax.annotate('', xy=(10, 0), xytext=(14, 2.7), arrowprops=dict(arrowstyle='->', lw=1))
ax.annotate('', xy=(10, 0), xytext=(14, 1.2), arrowprops=dict(arrowstyle='->', lw=1))
ax.annotate('', xy=(10, 0), xytext=(14, -0.3), arrowprops=dict(arrowstyle='->', lw=1))

# 添加标题
ax.text(8, 9.5, '异常评分机制系统流程图', ha='center', va='center', fontsize=16, weight='bold')

# 添加评分维度说明框
ax.add_patch(plt.Rectangle((10, 7), 5, 2, facecolor='lightgray', edgecolor='black'))
ax.text(12.5, 8.5, '评分维度说明', ha='center', va='center', fontsize=12, weight='bold')
ax.text(12.5, 8, '• 初始严重程度 (20%)', ha='center', va='center', fontsize=9)
ax.text(12.5, 7.5, '• 持续时间 (20%)', ha='center', va='center', fontsize=9)
ax.text(12.5, 7, '• 发生频率 (15%)', ha='center', va='center', fontsize=9)

# 添加权重说明框
ax.add_patch(plt.Rectangle((10, 5), 5, 1.5, facecolor='lightgray', edgecolor='black'))
ax.text(12.5, 6, '权重配置', ha='center', va='center', fontsize=12, weight='bold')
ax.text(12.5, 5.5, '可配置的权重系统', ha='center', va='center', fontsize=9)

plt.tight_layout()
plt.savefig('scoring_flowchart.png', dpi=300, bbox_inches='tight')
plt.show()